<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Metrics Calculation</title>
  </head>
  <body>
    <script>
      const values = [
        10, 0, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0,
        10, 10, 0, 10, 10, 10, 10, 10, 0, 10, 0, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 0, 0,
        10, 10, 0, 0, 10, 10, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
        0, 10, 10, 10, 10, 10, 10, 0, 0, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 0, 10,
        10, 10, 10, 10, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 10, 10,
        10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 0, 10, 0, 10, 10, 0, 10, 10,
        10, 0, 0, 0, 10, 10, 10, 0, 10, 10, 10, 0, 10, 10, 10, 0, 0, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 0,
        10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 0, 10, 0,
        10, 10, 10, 10, 10, 0, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
        10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
        10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 0, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10,
        10, 10, 0, 10, 10, 10, 10, 0, 10, 10, 10, 0, 10, 10, 10, 10, 10, 10, 0, 10, 10, 10, 10, 10, 0, 10, 10, 10, 0,
        10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 0, 10, 10, 10, 10, 10
      ]
      // 方法1：统计总错误次数和总正确次数
      function countTotals(values) {
        let totalErrors = 0;
        let totalCorrects = 0;

        values.forEach((value, index) => {
          if (value === 0) {
            totalErrors += 1;
          } else if (value === 10) {
            totalCorrects += 1;
          } else {
            console.warn(`发现非0或10的值: ${value} 在索引 ${index}`);
          }
        });

        return { totalErrors, totalCorrects };
      }

      // 方法2：统计连续错误段数和连续正确段数
      function countStreaks(values) {
        let totalErrorStreaks = 0;
        let totalCorrectStreaks = 0;
        let lastValue = null;

        values.forEach((value, index) => {
          if (value === 0) {
            if (lastValue !== 0) {
              totalErrorStreaks += 1;
            }
          } else if (value === 10) {
            if (lastValue !== 10) {
              totalCorrectStreaks += 1;
            }
          }
          lastValue = value;
        });

        return { totalErrorStreaks, totalCorrectStreaks };
      }

      // 方法3：统计在每次错误之前的正确次数和在每次正确之前的错误次数
      function countTransitions(values) {
        let sumCorrectBeforeError = 0;
        let countCorrectBeforeError = 0;
        let sumErrorBeforeCorrect = 0;
        let countErrorBeforeCorrect = 0;

        let currentErrorStreak = 0;
        let currentCorrectStreak = 0;

        values.forEach((value, index) => {
          if (value === 0) {
            if (currentCorrectStreak > 0) {
              sumCorrectBeforeError += currentCorrectStreak;
              countCorrectBeforeError += 1;
              currentCorrectStreak = 0;
            }
            currentErrorStreak += 1;
          } else if (value === 10) {
            if (currentErrorStreak > 0) {
              sumErrorBeforeCorrect += currentErrorStreak;
              countErrorBeforeCorrect += 1;
              currentErrorStreak = 0;
            }
            currentCorrectStreak += 1;
          }
        });

        return {
          sumCorrectBeforeError,
          countCorrectBeforeError,
          sumErrorBeforeCorrect,
          countErrorBeforeCorrect
        };
      }

      // 方法4：确定当前的连续状态和长度
      function getCurrentStreak(values) {
        let currentStreakType = null;
        let currentStreakLength = 0;

        if (values.length === 0) {
          return { currentStreakType, currentStreakLength };
        }

        // 从后往前找最后一个连续的段
        let lastValue = values[values.length - 1];
        currentStreakType = lastValue === 0 ? '错误' : '正确';
        currentStreakLength = 1;

        for (let i = values.length - 2; i >= 0; i--) {
          if (values[i] === lastValue) {
            currentStreakLength += 1;
          } else {
            break;
          }
        }

        return { currentStreakType, currentStreakLength };
      }

      // 方法5：计算平均值
      function computeAverages(totalErrors, totalCorrects, totalErrorStreaks, totalCorrectStreaks, sumCorrectBeforeError, countCorrectBeforeError, sumErrorBeforeCorrect, countErrorBeforeCorrect) {
        const averageConsecutiveErrors =
          totalErrorStreaks > 0 ? parseFloat((totalErrors / totalErrorStreaks).toFixed(2)) : 0;

        const averageConsecutiveCorrects =
          totalCorrectStreaks > 0 ? parseFloat((totalCorrects / totalCorrectStreaks).toFixed(2)) : 0;

        const averageCorrectBeforeError =
          countCorrectBeforeError > 0 ? parseFloat((sumCorrectBeforeError / countCorrectBeforeError).toFixed(2)) : 0;

        const averageErrorBeforeCorrect =
          countErrorBeforeCorrect > 0 ? parseFloat((sumErrorBeforeCorrect / countErrorBeforeCorrect).toFixed(2)) : 0;

        return {
          averageConsecutiveErrors,
          averageConsecutiveCorrects,
          averageCorrectBeforeError,
          averageErrorBeforeCorrect
        };
      }

      // 方法6：比较当前连续状态与平均值
      function compareStreak(currentStreakType, currentStreakLength, averageCorrectBeforeError, averageErrorBeforeCorrect) {
        let streakComparisonToAverageCorrectBeforeError = null;
        let streakComparisonToAverageErrorBeforeCorrect = null;

        if (currentStreakType === '正确') {
          if (averageCorrectBeforeError > 0) {
            const difference = currentStreakLength - averageCorrectBeforeError;
            const sign = difference > 0 ? '+' : '';
            streakComparisonToAverageCorrectBeforeError = difference !== 0
              ? `${difference > 0 ? '高于' : '低于'}平均水平 (差距: ${sign}${difference.toFixed(2)})`
              : `等于平均水平 (差距: 0.00)`;
          } else {
            streakComparisonToAverageCorrectBeforeError = '无平均水平数据';
          }
        }

        if (currentStreakType === '错误') {
          if (averageErrorBeforeCorrect > 0) {
            const difference = currentStreakLength - averageErrorBeforeCorrect;
            const sign = difference > 0 ? '+' : '';
            streakComparisonToAverageErrorBeforeCorrect = difference !== 0
              ? `${difference > 0 ? '高于' : '低于'}平均水平 (差距: ${sign}${difference.toFixed(2)})`
              : `等于平均水平 (差距: 0.00)`;
          } else {
            streakComparisonToAverageErrorBeforeCorrect = '无平均水平数据';
          }
        }

        return {
          streakComparisonToAverageCorrectBeforeError,
          streakComparisonToAverageErrorBeforeCorrect
        };
      }

      // 主方法：整合所有计算
      function calculateMetrics(values) {
        const totals = countTotals(values);
        const streaks = countStreaks(values);
        const transitions = countTransitions(values);
        const currentStreak = getCurrentStreak(values);
        const averages = computeAverages(
          totals.totalErrors,
          totals.totalCorrects,
          streaks.totalErrorStreaks,
          streaks.totalCorrectStreaks,
          transitions.sumCorrectBeforeError,
          transitions.countCorrectBeforeError,
          transitions.sumErrorBeforeCorrect,
          transitions.countErrorBeforeCorrect
        );
        const comparisons = compareStreak(
          currentStreak.currentStreakType,
          currentStreak.currentStreakLength,
          averages.averageCorrectBeforeError,
          averages.averageErrorBeforeCorrect
        );

        return {
          ...averages,
          totalErrors: totals.totalErrors,
          totalCorrects: totals.totalCorrects,
          totalErrorStreaks: streaks.totalErrorStreaks,
          totalCorrectStreaks: streaks.totalCorrectStreaks,
          currentStreakType: currentStreak.currentStreakType,
          currentStreakLength: currentStreak.currentStreakLength,
          streakComparisonToAverageCorrectBeforeError: comparisons.streakComparisonToAverageCorrectBeforeError,
          streakComparisonToAverageErrorBeforeCorrect: comparisons.streakComparisonToAverageErrorBeforeCorrect
        };
      }

      // 执行计算
      const metrics = calculateMetrics(values);

      // 输出结果
      console.log('平均连续错误次数:', metrics.averageConsecutiveErrors);
      console.log('平均连续正确次数:', metrics.averageConsecutiveCorrects);
      console.log('平均连续正确多少次之后可能会错误:', metrics.averageCorrectBeforeError);
      console.log('平均连续错误多少次之后可能会正确:', metrics.averageErrorBeforeCorrect);
      console.log('总错误次数:', metrics.totalErrors);
      console.log('总正确次数:', metrics.totalCorrects);
      console.log('总错误段数:', metrics.totalErrorStreaks);
      console.log('总正确段数:', metrics.totalCorrectStreaks);
      console.log('当前连续类型:', metrics.currentStreakType);
      console.log('当前连续长度:', metrics.currentStreakLength);
      console.log('当前连续正确与平均正确前错误对比:', metrics.streakComparisonToAverageCorrectBeforeError);
      console.log('当前连续错误与平均错误前正确对比:', metrics.streakComparisonToAverageErrorBeforeCorrect);
      console.log('总数验证 (应等于values.length):', metrics.totalErrors + metrics.totalCorrects === values.length);
    </script>
  </body>
</html>
